<?php


	require_once ($_SERVER['DOCUMENT_ROOT']."/Auditing/data/config.php");
	require_once ($_SERVER['DOCUMENT_ROOT']."/Auditing/data/DBConn.php");
	
	

Class InsertRule{
		
	var $rule_name;
	var $flag;
	var $field;
	var $logic;
	var $text_value;
	
	function InsertRule($rule_name,$flag,$field,$logic,$text_value){

		$this->rule_name = $rule_name;
		$this->flag = $flag;
		$this->field = $field;
		$this->logic = $logic;
		$this->text_value = $text_value;
		
	}
	
	
	function saveRule(){
		// Inserting in RuleMETA Data
	
	$returnVal=1; // Successfull insertion
	
	$query= "Insert into ota_rulemetadata (description,flag_id)". 
			"VALUES ('".$this->rule_name."',".$this->flag.")";

	//$result = TRUE;  // For Debugging Only
	$result = mysql_query($query); //or die('Error, insert query failed');
	
//	echo $query;
	
	if (!$result) {
		$returnVal = 0;	
    	die('Invalid query: ' . mysql_error());
	}
	else {
		
		$query= "Select max(rule_meta_data_id) from ota_rulemetadata";
	
		$result = mysql_query($query); 
		
		if ($result!=NULL) {
			while ($row = mysql_fetch_assoc($result)) {
				$rule_meta_data_id =  $row['max(rule_meta_data_id)'];
			}
		}	
		
	
			$returnVal = $this->insertIntoRuleSet($this->field,$rule_meta_data_id,$this->logic,$this->text_value);
						
	}		
		return $returnVal;	
}
	
function insertIntoRuleSet($field,$rule_meta_data_id,$logic,$text_value){

		for($i=0; $i<sizeof($field); $i++ ){

				$query= "insert into ota_ruleset (rule_meta_data_id,field_id,logic_id,value)". 
						"values (".$rule_meta_data_id.",".
						        $field[$i].",".
						        $logic[$i].",'".
								$text_value[$i]."')";		
//				echo $query;
				$result = mysql_query($query); 
	
				if (!$result) {
					$returnVal = 0;	
			    	die('Invalid query: ' . mysql_error());
				}
												
			}
			return $result;
		
	}

}

?>